//
// Created by macro on 2020/1/6.
//

#ifndef CPPLEARN_HELPER_H
#define CPPLEARN_HELPER_H

#endif //CPPLEARN_HELPER_H

#include <iostream>
#include <cassert>
using namespace std;

// 数组生成
namespace SortTestHelper{
    int* generateRandomArray(int n, int rangeL, int rangeR){
        assert(rangeL <= rangeR);
        int *arr = new int[n];
        srand(time(NULL));
        for (int i = 0; i < n; ++i) {
            arr[i] = rand()%(rangeL - rangeR + 1) + rangeL;
        }
        return arr;
    }


// 数组输出
    template<typename T>
    void printArray(T arr[], int n){
        for (int i = 0; i < n; ++i) {
            cout << arr[i] <<" ";
        }
        cout << endl;
    }

    // 排序是否成功
    template <typename T>
    bool isSorted(T arr[], int n){
        for (int i = 0; i < n-1; ++i) {
            if (arr[i] > arr[i+1])
                return false;
        }
        return true;
    }

    // 效率预测
    template <typename T>
    void testSort(string sortName, void(*sort)(T[],int), T arr[], int n){
        clock_t startTime  = clock();
        sort(arr,n);
        clock_t endTime = clock();
        assert(isSorted(arr,n));
        cout << sortName << " : " << double (endTime - startTime) /CLOCKS_PER_SEC<<" s"<<endl;
    }


}
